import pandas as pd
import numpy as np

t = pd.DataFrame(np.arange(8).reshape(2, 4), index=list("ab"), columns=list("WXYZ"))
print("0--", t)
print("1--", t.index, "2--", t.columns)

t.index = ["A", "B"] # 更改索引的值
print("2--", t)

t1 = t.reindex(["A", "D"])
print("3--", t)
print("4--", t1)

t2 = t.set_index("W") # 指定某一列作为index  删除了W
print("-"*20)
print(t2)
print("6--", t2.index)

t3 = t.set_index("W", drop=False) # 指定某一列作为index  没有删除W
print("-"*20)
print(t3)
print("6--", t3.index)

df_1 = pd.DataFrame(np.ones((2,4)), index=["A", "B"], columns=list("abcd"))
print("7---", df_1)
df_1.loc["A", "a"] = 100
print("7---", df_1)

print("8--", df_1["d"].unique(), "--8--", len(df_1["d"].unique())) # 去重
print("9--", df_1["a"].unique(), "--9--", list(df_1["a"].unique()))

print("10--",  df_1.set_index("a", "b")) # 只有a有效
print("11--",  df_1.set_index(["a", "b"])) # a  都有效果
print("12--",  df_1.set_index(["a", "b"]).index)
print("13--",  df_1.set_index(["a", "b", "c"], drop=False))

# 每一列有什么内容
a = pd.DataFrame({'a': range(7), "b": range(7,0,-1), 'c': ["one", "one", "one", "two", "two", "two", "two"], 'd': list("hjklmno")})

print("14---", a)
b = a.set_index(["c", "d"]) # 有两个索引的
print("-" * 10)
print(b)
print("15---", b.index)
c = b["a"]
print("16---", c)
print("17---", c["one"]["j"]) # 取one j 的值
print("18---", c["one"])

d = a.set_index(["d", "c"])["a"]
print("19---", d)
print("20---", d.swaplevel())
print("21---",  d.swaplevel()["one"]) # 取one的值
print("22---",  "") # TODO 取one j 的值
